<?php
/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2017/3/15
 * Time: 10:36
 */

namespace SDXApi\Service;


use SDXApi\Adapters\AlbumAdapter;
use SDXApi\Model\AlbumModel;
use SDXApi\Model\AlbumTemplateAlbumsModel;
use SDXApi\Model\AlbumTemplateGoodsModel;
use SDXApi\Model\AlbumTemplateModel;

/**
 * Class AlbumService
 * @package SDXApi\Service
 */
class AlbumService
{
	/**
	 * @var AlbumModel
	 */
	protected $albumModel;

    /**
     * @var AlbumTemplateModel
     */
    protected $albumTemplateModel;

    /**
     * @var AlbumTemplateAlbumsModel
     */
    protected $albumTemplateAlbumModel;

	/**
	 * @var int
	 */
	public $userId = 0;

	/**
	 * @var int
	 */
	public $wid = 8;


    /**
     * 注入实例
     * AlbumService constructor.
     */
    public function __construct()
	{
		$this->albumModel = new AlbumModel();
		$this->albumTemplateModel = new AlbumTemplateModel();
		$this->albumTemplateAlbumModel = new AlbumTemplateAlbumsModel();
	}

	/**
	 * 获取专辑数据
	 * Created by Jerry
	 * @param int $id
	 * @return array|bool
	 */
	public function getAlbumData($id = 0)
	{
		$data = $this->findAlbumById($id);

		$albumData = $this->checkAlbumData($data);

		if (empty($albumData)) {
			$albumData = array();
		}

		return $albumData;
	}

	/**
	 * 获取指定id专辑
	 * Created by Jerry
	 * @param int $id
	 * @return mixed
	 */
	protected function findAlbumById($id = 0)
	{
		return $this->albumModel->findAlbumById($id);
	}

	/**
	 * 处理专辑数据
	 * Created by Jerry
	 * @param array $data
	 * @return array|bool
	 */
	protected function checkAlbumData($data = array())
	{

		$templateData = $this->findAlbumTemplateByAlbumId($data['id']);

		if (empty($templateData)) {
			return false;
		}

		$templateData = $this->checkTemplateData($templateData);

		if (empty($templateData)) {
			$templateData = array();
		}

		$data['album_list'] = $templateData;

		$data['cover'] = $this->getAlbumCover($data['id']);

		return $data;
	}

	/**
	 * 获取专辑模版数据
	 * Created by Jerry
	 * @param int $templateId
	 * @return bool
	 */
	protected function findAlbumTemplateByAlbumId($templateId = 0)
	{
		if (empty($templateId)) {
			return false;
		}

		return $this->albumTemplateModel->findAlbumTemplateByAlbumId($templateId);
	}

	/**
	 * 处理模版数据
	 * Created by Jerry
	 * @param array $data
	 * @return array|bool
	 */
	protected function checkTemplateData($data = array())
	{
		if (empty($data) || ! is_array($data)) {
			return false;
		}

		$templateData = array();
		foreach ($data as $key => $value) {
			$arr = $this->album($value['type'])->handle($value, $this->userId, $this->wid);

			if (empty($arr)) {
				continue;
			}

			$templateData[] = $arr;
		}

		return $templateData;
	}

	/**
	 * 调用适配器处理
	 * Created by Jerry
	 * @param int $type
	 * @return AlbumAdapter
	 */
	protected function album($type = 0)
	{
		return new AlbumAdapter($type);
	}


	/**
	 * 获取封面图片
	 * Created by Jerry
	 * @param int $albumId
	 * @return string
	 */
	public function getAlbumCover($albumId = 0)
	{
		$cover = '';
		if (!empty($albumId)) {
			$cover = $this->albumModel->findAlbumCoverByAlbumId($albumId);
		}

		return $cover;
	}

    /**
     * @author Jerry
     * @return array|mixed
     */
    public function getArticleAlbums()
    {
        $album = $this->albumModel->findAlbums();

        if (empty($album)) {
            return array();
        }

        foreach ($album as $key => $value) {
            $cover = $this->albumTemplateAlbumModel->findAlbumFourByAlbumId($value['id']);

            if (empty($cover)) {
                unset($album[$key]);
                continue;
            }

            $album[$key]['cover_list'] = $cover;
        }

        $album = array_merge($album);

        return $album;
    }
}